//基于element-ui的el-form使用的正则判断

const Reg = {
	phone: /^1[3456789]\d{9}$/,
	userId: /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
	Email: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
	userName: /^[\u4E00-\u9FA5][\u4E00-\u9FA5|·]*[\u4E00-\u9FA5]$/,
	//字母开头，长度在6~18之间，只能包含字母、数字和下划线
	password: /^[a-zA-Z]\w{5,17}$/,
	number:/^\d+$|^\d*\.\d+$/g,
	//必须包含大小写字母和数字的组合，不能使用特殊字符，长度在8-10之间
	passwordStrong: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$/,
	cardID:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
}
//判断手机号码是否填写和格式是否错误
const checkPhone = msg => {
	return (rule, value, callback) => {
		if (!value) {
			return callback(new Error("请输入正确的手机号"));
		}
		if (!Reg.phone.test(value)) {
			return callback(new Error(msg));
		} else {
			return callback();
		}
	};
};

//判断用户身份证是否填写和格式是否错误
const checkUserId = msg => {
	return (rule, value, callback) => {
		if (!value) {
			return callback(new Error("身份证号不能为空"));
		}
		if (!Reg.userId.test(value)) {
			return callback(new Error(msg));
		} else {
			return callback();
		}
	};
};

//判断用户邮箱是否填写和格式是否错误
const checkEmail = msg => {
	return (rule, value, callback) => {
		if (!value) {
			return callback(new Error("邮箱不能为空"));
		}
		if (!Reg.Email.test(value)) {
			return callback(new Error(msg));
		} else {
			return callback();
		}
	};
};

//判断用户姓名是否填写和格式是否错误
const checkUserName = msg => {
	return (rule, value, callback) => {
		if (!value) {
			return callback(new Error("姓名不能为空"));
		}
		if (!Reg.userName.test(value)) {
			return callback(new Error(msg));
		} else {
			return callback();
		}
	};
};

//判断用户姓名是否为
const checkEmptyUserName = (msg = "帐号不能为空") => {
	return (rule, value, callback) => {
		console.log(value);
		if (!value) {
			return callback(new Error(msg));
		} else {
			return callback();
		}
	};
};

// 判断验证码不能为空
const checkEmptyCheckCode = (msg = "验证码不能为空") => {
	return (rule, value, callback) => {
		if (value == null || value == '') {
			return callback(new Error(msg));
		} else {
			return callback();
		}
	};
};

//判断是否为纯数字
const checkNumber = (msg = "字段必须是纯数字") => {
	return (rule, value, callback) => {
		let num = /^[0-9]+$/
		if (!value) {
			return callback(new Error("字段不能为空"));
		}
		if (!num.test(value)) {
			return callback(new Error(msg));
		} else {
			return callback();
		}
	};
};


//有特别需求可以自行加入正则，以及提示信息进行提示
const checkAny = (msg, reg) => {
	return (rule, value, callback) => {
		if (!reg.test(value)) {
			return callback(new Error(msg));
		} else {
			return callback();
		}
	};
};

let regexp = Reg

export { checkPhone, checkUserId, checkEmail, checkUserName, checkAny, checkEmptyCheckCode, regexp ,checkEmptyUserName,checkNumber};
